<?php

namespace app\api\controller;

use app\admin\model\Banner;
use app\admin\model\Goods;
use app\admin\model\GoodsCate;
use app\common\controller\Api;

/**
 * 积分商城接口
 */
class Shop extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 商品分类
     *
     */
    public function goodsCate()
    {
        $cate = GoodsCate::where(['status' => 1])
            ->field('id,title,image')
            ->limit(0, 6)
            ->select();
        foreach ($cate as &$a) {
            $a['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $a['image']));
        }
        $this->success('获取成功', $cate, 1);
    }

    /**
     * 分类列表
     *
     */
    public function goodsList()
    {
        $id = input('id/d', 1);
        if (empty($id)) {
            $this->error('请选择商品分类');
        }
        $list = Goods::where(['status' => 1, 'type' => 1, 'goods_cate_id' => $id])
            ->field('id,title,goods_cate_id,image,price,title,score,old_score,sell')
            ->select();
        if (!empty($list)) {
            foreach ($list as &$vs) {
                $vs['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $vs['image']));
            }
        }
        $this->success('获取成功', $list, 1);
    }

    /**
     * 列表详情
     *
     */
    public function goodsDetail()
    {
        $cate_id = input('cate_id/d', 4);
        if (empty($cate_id)) {
            $this->error('请选择商品');
        }
        $detail = Goods::where(['status' => 1, 'id' => $cate_id])
            ->field('id,image,old_score,score,price,title,is_postage,sell,service,content,address')
            ->find();
        if (!empty($detail)) {
            $detail['content'] = strip_tags($detail['content']);
            $detail['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $detail['image']));
        }
        $this->success('获取成功', $detail, 1);
    }

    /**
     * 商品推荐列表
     *
     */
    public function recommend()
    {
        $type = input('type/d', 1); //1=积分商品,2=拼团商品
        $is_top = input('is_top/d', 1); //1=推荐,-1=不推荐
        $list = Goods::where(['status' => 1, 'type' => $type, 'is_top' => $is_top])
            ->field('id,title,old_score,score,image,sell,price')
            ->limit(0, 4)
            ->select();
        foreach ($list as &$v) {
            $v['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $v['image']));
        }
        $this->success('获取成功', $list, 1);
    }
}
